package com.zx.scp.controller;

import com.zx.jd.util.JsonUtils;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.SUConstants;
import com.zx.scp.service.IIncomeService;
import com.zx.scp.service.model.YintongPayRequestModel;
import com.zx.scp.service.util.swift.XmlUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by bibei on 2017/6/2.
 * 51mayun支付
 */
@Controller
@RequestMapping("/income/mayunpay")
public class MayunIncomeController  {
	public Logger logger = Logger.getLogger(MayunIncomeController.class);
	@Autowired
	@Qualifier("mayunPayIncomeServiceImpl")
	private IIncomeService incomeService ;

	/**
	 * 通知支付
	 * @param request
	 * @return
	 */
	@RequestMapping("/nofityPay")
	public void nofityPay(HttpServletRequest request, HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			String string=XmlUtils.parseRequst(request);
			logger.info("51码云通知内容：" + string);
			System.out.println("51码云通知内容：" + string);
			//string=
			Map<String,Object> map= JsonUtils.jsonToBean(string,Map.class) ;
			if(!map.isEmpty()){
				String status = (String) map.get("status");
				if( "1".equals(status)){
					Map responseResult=incomeService.payNotify((Map) map.get("data"));
					response.getWriter().print(responseResult.get(SUConstants.MSG_KEY));
					return;
				}
			}
			response.getWriter().print("fail");
		} catch (Exception e) {
			logger.debug("51码云订单通知处理异常",e);
			try {
				response.getWriter().print("fail");
			} catch (Exception e1){
			}
		}
	}

	/**
	 * 51码云获取支付信息
	 * @param  orderResponseDomain
	 * @param request
	 * @return
	 */
	@RequestMapping("/startPay")
	@ResponseBody
	public Map startPay(YintongPayRequestModel orderResponseDomain, HttpServletRequest request, HttpServletResponse response){
		try {
//			orderResponseDomain.setOrderId(PmpUtils.getRechargeOrderNum());
//			orderResponseDomain.setAmount("0.01");
//			orderResponseDomain.setPayType("1");
			return  incomeService.startPay(request,orderResponseDomain);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("51码云获取支付信息异常",e);
			return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
		}
	}

	/**
	 * 微信公众号支付
	 * @param orderResponseDomain
	 * @param request
	 * @return
	 */
	@RequestMapping("/winxinJsPay")
	@ResponseBody
	public Map winxinJsPay(YintongPayRequestModel orderResponseDomain,HttpServletRequest request){
		try {
//			orderResponseDomain.setOrderId(PmpUtils.getRechargeOrderNum());
//			orderResponseDomain.setAmount("0.01");
//			orderResponseDomain.setPayType("2");
//			orderResponseDomain.setOpenId("ouMoRuCkWzSOiP78xOf7MWHR7wLk");
			return  incomeService.winxinJsPay(request,orderResponseDomain);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("51码云获取支付信息异常",e);
			return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
		}
	}
}
